package database;

import java.util.ArrayList;
import java.util.List;

import news.News;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class NewsDatabaseService {

	private SQLiteDatabase db;
	
	public NewsDatabaseService(SQLiteDatabase db) {
		this.db = db;
	}
	
	public void InsertNews(List<News> news)
	{
		db.delete(INewsTable.NAME, null, null);
	
		for (News item : news) {
			
			ContentValues values = new ContentValues();
			values.put(INewsTable.AUTHOR, item.getAuthor());
			values.put(INewsTable.MSG, item.getMessage());
			values.put(INewsTable.DATE, item.getDate());
			
			db.insert(INewsTable.NAME, null, values);
		}	
	}
	
	public ArrayList<News> GetNews()
	{
		Cursor cursor = db.query(INewsTable.NAME, null, null, null, null, null, null);		
		ArrayList<News> news = new ArrayList<News>();
		
		while(cursor.moveToNext())
		{
			news.add(new News(
					cursor.getString(cursor.getColumnIndex(INewsTable.DATE)),
					cursor.getString(cursor.getColumnIndex(INewsTable.AUTHOR)),
					cursor.getString(cursor.getColumnIndex(INewsTable.MSG))));
		}
		
		return news;
	}

	public void close() {
		db.close();
	}
}
